package com.bulletelc.apollopre.module.main.center.invoice.list;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.InvoiceBean;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/29
 * desc  :
 */

public class InvoiceAdapter extends RecyclerView.Adapter<InvoiceAdapter.ViewHolder> implements View.OnClickListener {

    private LayoutInflater inflater;

    private List<InvoiceBean> invoiceBeans;

    private boolean isChoose;

    private OnInvoiceEditClickListener onEditListener;

    private OnDelClickListener onRemoveListener;

    private OnCardViewClickListener onCardViewClickListener;

    private OnCheckboxClickListener onCheckboxClickListener;

    public InvoiceAdapter(List<InvoiceBean> invoiceBeans, boolean isChoose) {
        this.invoiceBeans = invoiceBeans;
        this.isChoose = isChoose;
    }

    public void setOnInvoiceEditListener(OnInvoiceEditClickListener listener) {
        this.onEditListener = listener;
    }

    public void setOnInvoiceRemoveListener(OnDelClickListener listener) {
        this.onRemoveListener = listener;
    }

    public void setOnCardViewClickListener(OnCardViewClickListener onCardViewClickListener) {
        this.onCardViewClickListener = onCardViewClickListener;
    }

    public void setOnCheckboxClickListener(OnCheckboxClickListener onCheckboxClickListener) {
        this.onCheckboxClickListener = onCheckboxClickListener;
    }

    @Override
    public InvoiceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());

        ViewHolder viewHolder = new ViewHolder(inflater.inflate(R.layout.invoice_list_item, parent, false));

        viewHolder.cardView.setOnClickListener(this);
        viewHolder.tvEdit.setOnClickListener(this);
        viewHolder.tvDel.setOnClickListener(this);
        viewHolder.cbDefault.setOnClickListener(this);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(InvoiceAdapter.ViewHolder holder, final int position) {
        InvoiceBean invoice = invoiceBeans.get(position);
        if(getType(invoice.getType())=="公司"){
            holder.layout_view_personal.setVisibility(View.INVISIBLE);
            holder.layout_view_personal.setVisibility(View.GONE);
            holder.tvName.setText(invoice.getCompany_name());
            holder.tvType.setText(getType(invoice.getType()));
            holder.tvTaxNo.setText("纳税人识别号  "+invoice.getTax_no());
            holder.tvBankName.setText("开户行  "+invoice.getBank_name());
            holder.tvBankCardNo.setText("账号  "+invoice.getBank_card_no());
            holder.tvPhone.setText("手机  "+invoice.getPhone_no());
            holder.tvAddress.setText("地址  "+invoice.getAddress());
            holder.cardView.setTag(position);
            holder.tvEdit.setTag(position);
            holder.tvDel.setTag(position);
            holder.cbDefault.setTag(position);
        }else {
            holder.layout_view_personal.setVisibility(View.VISIBLE);
            holder.layout_view_compy.setVisibility(View.INVISIBLE);
            holder.layout_view_compy.setVisibility(View.GONE);

            holder.ptvName.setText(invoice.getCompany_name());
            holder.ptvType.setText(getType(invoice.getType()));
            holder.ptvPhone.setText("手机"+invoice.getPhone_no());
            holder.ptvAddress.setText("地址  "+invoice.getAddress());
            holder.cardView.setTag(position);
            holder.tvEdit.setTag(position);
            holder.tvDel.setTag(position);
            holder.cbDefault.setTag(position);
        }

        if (isChoose) {
            holder.llEdit.setVisibility(View.GONE);
        } else {
            holder.llEdit.setVisibility(View.VISIBLE);
            holder.cbDefault.setChecked(isDefault(invoice.getIs_default()));
        }

    }

    @Override
    public int getItemCount() {
        return invoiceBeans.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()) {

            case R.id.cardView://点击整个cardView
                if (isChoose) {
                    if (onCardViewClickListener != null) {
                        onCardViewClickListener.onCardViewClick(v, position);
                    }
                }
                break;

            case R.id.tvEdit: //编辑
                if (onEditListener != null) {
                    onEditListener.onEditClick(v, position);
                }
                break;

            case R.id.tvDel:  //删除
                if (onRemoveListener != null) {
                    onRemoveListener.onDelClick(v,position);
                }
                break;

            case R.id.cbDefault: //设置默认
                if (onCheckboxClickListener != null) {
                    onCheckboxClickListener.onCheckboxClick(position);
                }
                break;
        }
    }


    /**
     * 判断是否默认选中
     * @param isDefault
     * @return
     */
    private boolean isDefault(int isDefault) {
        return isDefault == 1 ? true : false;
    }

    /**
     * 获取发票类型: 个人/公司
     * @param type
     * @return
     */
    private String getType(int type) {
        return type == 1 ? "公司" : "个人";
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout cardView,layout_view_personal,layout_view_compy;
        TextView tvName, tvType, tvTaxNo, tvBankName, tvBankCardNo, tvPhone, tvAddress, tvEdit, tvDel,ptvName,ptvType,ptvPhone,ptvAddress;
        CheckBox cbDefault;
        LinearLayout llEdit;

        public ViewHolder(View itemView) {
            super(itemView);
            layout_view_compy=itemView.findViewById(R.id.layout_view_compy);
            layout_view_personal=itemView.findViewById(R.id.layout_view_personal);
            ptvName=itemView.findViewById(R.id.ptvName);
            ptvType=itemView.findViewById(R.id.ptvType);
            ptvPhone=itemView.findViewById(R.id.ptvPhone);
            ptvAddress=itemView.findViewById(R.id.ptvAddress);

            cardView = itemView.findViewById(R.id.cardView);
            tvName = itemView.findViewById(R.id.tvName);
            tvType = itemView.findViewById(R.id.tvType);
            tvTaxNo = itemView.findViewById(R.id.tvTaxNo);
            tvBankName = itemView.findViewById(R.id.tvBankName);
            tvBankCardNo = itemView.findViewById(R.id.tvBankCardNo);
            tvPhone = itemView.findViewById(R.id.tvPhone);
            tvAddress = itemView.findViewById(R.id.tvAddress);
            tvEdit = itemView.findViewById(R.id.tvEdit);
            tvDel = itemView.findViewById(R.id.tvDel);
            cbDefault = itemView.findViewById(R.id.cbDefault);
            llEdit = itemView.findViewById(R.id.llEdit);
        }
    }
    public class PViewHolder extends RecyclerView.ViewHolder {
        LinearLayout cardView;
        TextView tvName, tvType, tvTaxNo, tvBankName, tvBankCardNo, tvPhone, tvAddress, tvEdit, tvDel;
        CheckBox cbDefault;
        LinearLayout llEdit;

        public PViewHolder(View itemView) {
            super(itemView);

            cardView = itemView.findViewById(R.id.cardView);
            tvName = itemView.findViewById(R.id.tvName);
            tvType = itemView.findViewById(R.id.tvType);
            tvTaxNo = itemView.findViewById(R.id.tvTaxNo);
            tvBankName = itemView.findViewById(R.id.tvBankName);
            tvBankCardNo = itemView.findViewById(R.id.tvBankCardNo);
            tvPhone = itemView.findViewById(R.id.tvPhone);
            tvAddress = itemView.findViewById(R.id.tvAddress);
            tvEdit = itemView.findViewById(R.id.tvEdit);
            tvDel = itemView.findViewById(R.id.tvDel);
            cbDefault = itemView.findViewById(R.id.cbDefault);
            llEdit = itemView.findViewById(R.id.llEdit);
        }
    }
    public interface OnInvoiceEditClickListener {
        void onEditClick(View view, int position);
    }

    public interface OnDelClickListener {
        void onDelClick(View view, int position);
    }

    public interface OnCardViewClickListener {
        void onCardViewClick(View view, int position);
    }

    public interface OnCheckboxClickListener{

        void onCheckboxClick(int position);
    }
}
